//
//  settingViewController.swift
//  todoApp
//
//  Created by lili on 15/3/26.
//  Copyright (c) 2015年 ll studio. All rights reserved.
//

import UIKit



let secondViewController:SKStoreProductViewController=SKStoreProductViewController()

class settingViewController: UIViewController,SKPaymentTransactionObserver,SKProductsRequestDelegate {

    var product:SKProduct!
    
    @IBOutlet weak var productInfo: UILabel!
    @IBOutlet weak var adlabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        SKPaymentQueue.defaultQueue().addTransactionObserver(self)
        self.getProductInfo()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
   /* @IBAction func removeAD(sender: UIButton) {
        
        secondViewController.delegate=self
        
        var someitunesid:string=""
        var productparameters=[SKStoreProductParameterITunesItemIdentifier:someitunesid]
        secondViewController.loadProductWithParameters(productparameters, {success: Bool!, error:NSError!) -> Void in
            if success {
                self.presentViewController(secondViewController, animated: true, completion: nil)
                
            } else {
                NSLog("%@",error)
            }
        })
        
        
    }
*/
    func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
        let products=response.products
        
        
        if (products.count != 0)
        {
            product=products[0] as! SKProduct

            productInfo.text=product.localizedTitle+"\n"+product.localizedDescription
            
        }

    }

    func getProductInfo() {
        
        if(SKPaymentQueue.canMakePayments()) {                              //check itunes account
            let productID:NSSet=NSSet(object:"com.lili.removeAD")
            let request:SKProductsRequest=SKProductsRequest(productIdentifiers: productID as! Set<String>)
            request.delegate=self
            request.start()
        }
    
    }
    
    @IBAction func buyProduct(sender: AnyObject) {
        
        if  product != nil
        {
            let payment=SKPayment(product: product)
            
            SKPaymentQueue.defaultQueue().addPayment(payment)
        }

    }
    
    func productViewControllerDidFinish(viewController: SKStoreProductViewController!) {
        
        secondViewController.dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    
    
    func openLevel2() {
        
        adlabel.enabled=true
        adlabel.hidden=false

    }
    
    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]){
        
        for transaction:AnyObject in transactions {
            if let trans:SKPaymentTransaction=transaction as? SKPaymentTransaction{
                switch trans.transactionState {
                case .Purchased:
                    self.openLevel2()
                    SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                    break
                case .Failed:
                    SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                    break
                case .Restored:
                    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
                    break
                default:
                    break
                }
            }
        }
    }

    func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]){
        //todo 删除订单
    }
    
    func paymentQueue(queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: NSError){
        //todo 恢复已购买
    }
    
    func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue){
        //todo
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
